let _token = $('input[name=_token]').eq(0).val()
// 接口添加
;(function ($, global) {
  let $form = $('#api-form')
  let $submit = $form.find('button.submit')
  let $inputTable = $('#input-table')
  let $outputTable = $('#output-table')
  let $errorTable = $('#error-table')
  let $projectSelect = $form.find('#project-select')
  let $categorySelect = $form.find('#category-select')

  let verify = {}

  let fnInitAddReq = function () {
    return new FormData($form[0])
  }

  let fnAddAjax = function (data) {
    fnUploadAjax('POST', 'api/add', data, 'json', fnCommonAddSuccess, fnCommonAddError)
  }

  let projectsObj = $.parseJSON(projects)
  let fnTrigger = function () {
    $submit.click(function () {
      let req = fnInitAddReq($form)
      if (reqVerify(req, verify)) {
        //   fnBtnDisable($(this))
        fnLoading()
        fnAddAjax(req)
      }
    })

    $projectSelect.change(function () {
      $categorySelect.children().remove()
      let projectid = $(this).val()
      for (let key in projectsObj) {
        if (projectid == projectsObj[key]['id']) {
          let categorys = projectsObj[key]['categorys']
          fnCategorySelectFill(categorys)
        }
      }
    })
  }

  let fnAutoChange = function() {
    $categorySelect.children().remove()
    let projectid = $projectSelect.val()
    for (let key in projectsObj) {
      if (projectid == projectsObj[key]['id']) {
        let categorys = projectsObj[key]['categorys']
        fnCategorySelectFill(categorys)
      }
    }
  }

  
  let fnCategorySelectFill = function (cats) {
    let options = ''
    for (let index = 0; index < cats.length; index++) {
      options += '<option value="' + cats[index]['id'] + '">' + cats[index]['title'] + '</option>'
    }
    $categorySelect.children().remove()
    $categorySelect.append(options)
  }
  
  let fnAppendInputTr = function () {
    let $tr = $(inputTr)
    $inputTable.find('tbody').append($tr)
  }
  
  let fnAppendOutputTr = function () {
    let $tr = $(outputTr)
    $outputTable.find('tbody').append($tr)
  }

  let fnAppendErrorTr = function () {
    let $tr = $(errorTr)
    $errorTable.find('tbody').append($tr)
  }
  
  let fnRemoveTr = function (e) {
    $(e).parents('tr').remove()
  }
  window.fnAppendInputTr = fnAppendInputTr
  window.fnAppendOutputTr = fnAppendOutputTr
  window.fnAppendErrorTr = fnAppendErrorTr
  window.fnRemoveTr = fnRemoveTr
  
  fnTrigger()
  fnAutoChange();
}(jQuery, window))

// 接口修改
;(function ($, global) {
  let $form = $('#api-edit-form')
  let $submit = $form.find('button.submit')

  let verify = {}

  let fnInitReq = function () {
    return new FormData($form[0])
  }

  let fnAjax = function (data) {
    fnUploadAjax('POST', 'api/edit?id=' + data.get('id'), data, 'json', fnCommonEditSuccess, fnCommonEditError)
  }

  let fnTrigger = function () {
    $submit.click(function () {
      let req = fnInitReq($form)
      if (reqVerify(req, verify)) {
        //   fnBtnDisable($(this))
        fnLoading()
        fnAjax(req)
      }
    })
  }

  fnTrigger()
}(jQuery, window))

// 删除输入参数
;(function ($, global) {
  let $span = $('.input-del')

  let fnInitReq = function ($del) {
    return {
      id: $del.attr('data-id'),
      _token: _token
    }
  }

  let fnAjax = function (data) {
    fnApiAjax('POST', 'inputparam/del?id=' + data.id, data, 'json', fnSuccess, fnError)
  }

  let fnSuccess = function (data) {
    layer.msg('删除成功', {icon: 1})
    $('#inputparam-' + data.id).fadeTo('slow', 0.01, function () {
      $(this).slideUp('slow', function () {
        $(this).remove()
      })
    })
  }

  let fnError = function () {}

  let fnTrigger = function () {
    $span.click(function () {
      let req = fnInitReq($(this))
      fnDelConfirm(req)
    })
  }

  let fnDelConfirm = function (data) {
    layer.confirm('确定删除此项？', {
      btn: ['确定', '取消'] // 按钮
    }, function () {
      fnLoading()
      fnAjax(data)
    }, function () {})
  }
  fnTrigger()
}(jQuery, window))

// 删除输出参数
;(function ($, global) {
  let $span = $('.output-del')

  let fnInitReq = function ($del) {
    return {
      id: $del.attr('data-id'),
      _token: _token
    }
  }

  let fnAjax = function (data) {
    fnApiAjax('POST', 'outputparam/del?id=' + data.id, data, 'json', fnSuccess, fnError)
  }

  let fnSuccess = function (data) {
    layer.msg('删除成功', {icon: 1})
    $('#outputparam-' + data.id).fadeTo('slow', 0.01, function () {
      $(this).slideUp('slow', function () {
        $(this).remove()
      })
    })
  }

  let fnError = function () {}

  let fnTrigger = function () {
    $span.click(function () {
      let req = fnInitReq($(this))
      fnDelConfirm(req)
    })
  }

  let fnDelConfirm = function (data) {
    layer.confirm('确定删除此项？', {
      btn: ['确定', '取消'] // 按钮
    }, function () {
      fnLoading()
      fnAjax(data)
    }, function () {})
  }
  fnTrigger()
}(jQuery, window))

// 删除输出参数
;(function ($, global) {
  let $span = $('.apierror-del')

  let fnInitReq = function ($del) {
    return {
      id: $del.attr('data-id'),
      _token: _token
    }
  }

  let fnAjax = function (data) {
    fnApiAjax('POST', 'apierror/del?id=' + data.id, data, 'json', fnSuccess, fnError)
  }

  let fnSuccess = function (data) {
    layer.msg('删除成功', {icon: 1})
    $('#apierror-' + data.id).fadeTo('slow', 0.01, function () {
      $(this).slideUp('slow', function () {
        $(this).remove()
      })
    })
  }

  let fnError = function () {}

  let fnTrigger = function () {
    $span.click(function () {
      let req = fnInitReq($(this))
      fnDelConfirm(req)
    })
  }

  let fnDelConfirm = function (data) {
    layer.confirm('确定删除此项？', {
      btn: ['确定', '取消'] // 按钮
    }, function () {
      fnLoading()
      fnAjax(data)
    }, function () {})
  }
  fnTrigger()
}(jQuery, window))